package com.anlogic.sdk.ui.tests.uc3.projectmgr;

import static org.junit.Assert.*;

import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.anlogic.sdk.env.utils.EnvCheckUtils;
import com.anlogic.sdk.ui.tests.utils.WorkspaceUtils;
import com.anlogic.sdk.utils.EnvUtils;
import com.anlogic.sdk.utils.UIUtils;
import com.anlogic.sdk.workspace.FDWorkspace;

public class UC3_TC002_Test {
	
	private static SWTWorkbenchBot bot;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {

		bot = new SWTWorkbenchBot();
		SWTBotPreferences.TIMEOUT = 10000;
		SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
		bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"), 90 * 1000);
	//	SWTBotPreferences.PLAYBACK_DELAY = 10;
		
	}

	@Before
	public void setUp() throws Exception {
		WorkspaceUtils.resetworkbench(bot);
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		
	}


	@After
	public void tearDown() throws Exception {
		
		WorkspaceUtils.cleanWorkspace();
	}

	@Test
	public void resetsf1ProjectTest() throws Exception{
		bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"), 60 * 1000);

		bot.waitUntil(new DefaultCondition() {
			@Override
			public boolean test() throws Exception {
				// 检查菜单是否可用
				return bot.menu("File").menu("New").menu("Project...").isEnabled();
			}

			@Override
			public String getFailureMessage() {
				return "Menu 'Platform Project' is not available or not enabled.";
			}
		}, 60 * 1000);
		bot.menu("File").menu("New").menu("Project...").click();
		bot.tree().getTreeItem("C/C++").doubleClick();

		bot.tree().getTreeItem("C/C++").getNode("C/C++ Project").select();
		bot.tree().getTreeItem("C/C++").getNode("C/C++ Project").doubleClick();
		bot.table().select(0);
		bot.table().getTableItem(0).doubleClick();
		bot.textWithLabel("&Project name:").setText("sf1");
		bot.tree().getTreeItem("Executable").getNode("RISCV C Project (SF1)").select();
		bot.button("Next >").click();
		bot.button("Finish").click();
		bot.viewByTitle("Project Explorer").bot().tree().select("sf1").contextMenu("Reset Project").click();
		bot.comboBox().setSelection("sf1");
		bot.button("Reset").click();
		bot.button("Close").click();
		bot.tree().getTreeItem("sf1").select();
		
		bot.tree().getTreeItem("sf1").doubleClick();
	}
	
	@Test
	public void resetsoftcoreProjectTest() throws Exception{
		bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"), 60 * 1000);

		bot.waitUntil(new DefaultCondition() {
			@Override
			public boolean test() throws Exception {
				// 检查菜单是否可用
				return bot.menu("File").menu("New").menu("Project...").isEnabled();
			}

			@Override
			public String getFailureMessage() {
				return "Menu 'Platform Project' is not available or not enabled.";
			}
		}, 60 * 1000);
		bot.menu("File").menu("New").menu("Project...").click();
		bot.tree().getTreeItem("C/C++").doubleClick();

		bot.tree().getTreeItem("C/C++").getNode("C/C++ Project").select();
		bot.tree().getTreeItem("C/C++").getNode("C/C++ Project").doubleClick();
		bot.table().select(0);
		bot.table().getTableItem(0).doubleClick();
		bot.textWithLabel("&Project name:").setText("riscv");
		bot.tree().getTreeItem("Executable").getNode("RISCV C Project (Softcore)").select();
		bot.button("Next >").click();
		bot.button("Finish").click();
		bot.viewByTitle("Project Explorer").bot().tree().select("riscv").contextMenu("Reset Project").click();
		bot.comboBox().setSelection("softcore");
		bot.button("Reset").click();
		bot.button("Close").click();
		bot.tree().getTreeItem("riscv").select();
		
		bot.tree().getTreeItem("riscv").doubleClick();
	}
	
	@Test
	public void resetDr1m90ProjectTest() throws Exception{
		bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"),90000);
		

		 bot.waitUntil(new DefaultCondition() {
	            @Override
	            public boolean test() throws Exception {
	                // 检查菜单是否可用
	                return bot.menu("File").menu("New").menu("Platform Project").isEnabled();
	            }

	            @Override
	            public String getFailureMessage() {
	                return "Menu 'Platform Project' is not available or not enabled.";
	            }
	        },60*1000);

	        // 如果菜单项已经可用，进行点击
	        bot.menu("File").menu("New").menu("Platform Project").click();

		final SWTBotShell[] emptyTitleShell = new SWTBotShell[1];

		bot.waitUntil(new DefaultCondition() {
			public boolean test() {
				for (SWTBotShell shell : bot.shells()) {
					if ("".equals(shell.getText()) && shell.isOpen()) {
						emptyTitleShell[0] = shell;
						return true;
					}
				}
				return false;
			}

			public String getFailureMessage() {
				return "no find shell";
			}
		}, 10000);

		emptyTitleShell[0].activate();
		SWTBotShell activeShell = bot.activeShell();
		activeShell.activate();

		bot.textWithLabel("Project Name:").setText("platform");
		bot.comboBox().setSelection("AD101V20");
		bot.comboBox(1).setSelection("standalone");
		bot.comboBox(2).setSelection("apu-0");
		bot.button("Finish").click();
		bot.waitUntil(Conditions.shellCloses(emptyTitleShell[0]), 120000); // 最长等 2 分钟


		bot.viewByTitle("Project Explorer").show();
		SWTBotShell activeShell2 = bot.shell("data - FutureDynasty");
		activeShell2.activate();
		bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"),90000);
		// === App Project ===

		 bot.waitUntil(new DefaultCondition() {
	            @Override
	            public boolean test() throws Exception {
	                // 检查菜单是否可用
	                return bot.menu("File").menu("New").menu("Application Project").isEnabled();
	            }

	            @Override
	            public String getFailureMessage() {
	                return "Menu 'Platform Project' is not available or not enabled.";
	            }
	        },60*1000);
		bot.menu("File").menu("New").menu("Application Project").click();
		final SWTBotShell[] emptyTitleShell1 = new SWTBotShell[1];

		bot.waitUntil(new DefaultCondition() {
			public boolean test() {
				System.out.println("llctest");
				for (SWTBotShell shell : bot.shells()) {
					if ("".equals(shell.getText()) && shell.isOpen()) {
						emptyTitleShell1[0] = shell;
						return true;
					}
				}
				return false;
			}

			public String getFailureMessage() {
				return "no find shell";
			}
		}, 10000);

		//
		emptyTitleShell1[0].activate();

		bot.textWithLabel("Project Name:").setText("app");
		bot.radio("C").click();
		bot.comboBox(1).setSelection("64-bit");
		bot.comboBox(0).setSelection("platform");
		//bot.list().select(appName);

		 bot.waitUntil(Conditions.widgetIsEnabled(bot.button("Finish")), 30000);
		 bot.button("Finish").click();
		 bot.waitUntil(Conditions.shellCloses(emptyTitleShell1[0]), 120000);
		bot.viewByTitle("Project Explorer").show();
		bot.viewByTitle("Project Explorer").bot().tree().select("app").contextMenu("Reset Project").click();

		bot.comboBox().setSelection("dr1m90");
		bot.button("Reset").click();
		bot.button("Close").click();

	}
	
	
	@Test
	public void resetDr1v90ProjectTest() throws Exception{
	
	
	bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"),90000);
	

	 bot.waitUntil(new DefaultCondition() {
           @Override
           public boolean test() throws Exception {
               // 检查菜单是否可用
               return bot.menu("File").menu("New").menu("Platform Project").isEnabled();
           }

           @Override
           public String getFailureMessage() {
               return "Menu 'Platform Project' is not available or not enabled.";
           }
       },60*1000);

       // 如果菜单项已经可用，进行点击
       bot.menu("File").menu("New").menu("Platform Project").click();
	final SWTBotShell[] emptyTitleShell = new SWTBotShell[1];
	
	
	

	bot.waitUntil(new DefaultCondition() {
		public boolean test() {
			for (SWTBotShell shell : bot.shells()) {
				if ("".equals(shell.getText()) && shell.isOpen()) {
					emptyTitleShell[0] = shell;
					return true;
				}
			}
			return false;
		}

		public String getFailureMessage() {
			return "no find shell";
		}
	}, 10000);

	emptyTitleShell[0].activate();
	SWTBotShell activeShell = emptyTitleShell[0].activate();
	activeShell.activate();

	bot.textWithLabel("Project Name:").setText("platform");
	bot.comboBox().setSelection("AD102V20");
	bot.comboBox(1).setSelection("standalone");
	bot.comboBox(2).setSelection("rpu");
	bot.button("Finish").click();
	bot.waitUntil(Conditions.shellCloses(emptyTitleShell[0]), 120000); // 最长等 2 分钟


	bot.viewByTitle("Project Explorer").show();
	SWTBotShell activeShell2 = bot.shell("data - FutureDynasty");
	activeShell2.activate();
	bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"),90000);
	// === App Project ===
	
	 bot.waitUntil(new DefaultCondition() {
           @Override
           public boolean test() throws Exception {
               // 检查菜单是否可用
               return bot.menu("File").menu("New").menu("Application Project").isEnabled();
           }

           @Override
           public String getFailureMessage() {
               return "Menu 'Platform Project' is not available or not enabled.";
           }
       },60*1000);
	bot.menu("File").menu("New").menu("Application Project").click();
	final SWTBotShell[] emptyTitleShell1 = new SWTBotShell[1];

	bot.waitUntil(new DefaultCondition() {
		public boolean test() {
			System.out.println("llctest");
			for (SWTBotShell shell : bot.shells()) {
				if ("".equals(shell.getText()) && shell.isOpen()) {
					emptyTitleShell1[0] = shell;
					return true;
				}
			}
			return false;
		}

		public String getFailureMessage() {
			return "no find shell";
		}
	}, 10000);

	//
	emptyTitleShell1[0].activate();

	bot.textWithLabel("Project Name:").setText("app");
	bot.radio("C").click();
	// bot.comboBox(1).setSelection("64-bit");
	bot.comboBox(0).setSelection("platform");
	

	 bot.waitUntil(Conditions.widgetIsEnabled(bot.button("Finish")), 30000);
	 bot.button("Finish").click();

	bot.viewByTitle("Project Explorer").show();

	bot.viewByTitle("Project Explorer").bot().tree().select("app").contextMenu("Reset Project").click();

	bot.comboBox().setSelection("dr1v90");
	bot.button("Reset").click();
	bot.button("Close").click();
	}
	
	
	@Test
	public void resetph1p35ProjectTest() throws Exception{
		bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"),90000);
		

		 bot.waitUntil(new DefaultCondition() {
	            @Override
	            public boolean test() throws Exception {
	                // 检查菜单是否可用
	                return bot.menu("File").menu("New").menu("Platform Project").isEnabled();
	            }

	            @Override
	            public String getFailureMessage() {
	                return "Menu 'Platform Project' is not available or not enabled.";
	            }
	        },60*1000);

	        // 如果菜单项已经可用，进行点击
	        bot.menu("File").menu("New").menu("Platform Project").click();
		final SWTBotShell[] emptyTitleShell = new SWTBotShell[1];

		bot.waitUntil(new DefaultCondition() {
			public boolean test() {
				for (SWTBotShell shell : bot.shells()) {
					if ("".equals(shell.getText()) && shell.isOpen()) {
						emptyTitleShell[0] = shell;
						return true;
					}
				}
				return false;
			}

			public String getFailureMessage() {
				return "no find shell";
			}
		}, 10000);

		emptyTitleShell[0].activate();
		SWTBotShell activeShell = bot.activeShell();
		activeShell.activate();

		bot.textWithLabel("Project Name:").setText("platform");
		bot.comboBox().setSelection("PH1P35");
		bot.comboBox(1).setSelection("standalone");
		bot.comboBox(2).setSelection("rpu");
		bot.button("Finish").click();
		bot.waitUntil(Conditions.shellCloses(emptyTitleShell[0]), 120000); // 最长等 2 分钟


		bot.viewByTitle("Project Explorer").show();
		SWTBotShell activeShell2 = bot.shell("data - FutureDynasty");
		activeShell2.activate();
		bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"),90000);
		// === App Project ===

		 bot.waitUntil(new DefaultCondition() {
	            @Override
	            public boolean test() throws Exception {
	                // 检查菜单是否可用
	                return bot.menu("File").menu("New").menu("Application Project").isEnabled();
	            }

	            @Override
	            public String getFailureMessage() {
	                return "Menu 'Platform Project' is not available or not enabled.";
	            }
	        },60*1000);
		bot.menu("File").menu("New").menu("Application Project").click();
		final SWTBotShell[] emptyTitleShell1 = new SWTBotShell[1];

		bot.waitUntil(new DefaultCondition() {
			public boolean test() {
				System.out.println("llctest");
				for (SWTBotShell shell : bot.shells()) {
					if ("".equals(shell.getText()) && shell.isOpen()) {
						emptyTitleShell1[0] = shell;
						return true;
					}
				}
				return false;
			}

			public String getFailureMessage() {
				return "no find shell";
			}
		}, 10000);

		//
		emptyTitleShell1[0].activate();

		bot.textWithLabel("Project Name:").setText("app");
		bot.radio("C").click();
		// bot.comboBox(1).setSelection("64-bit");
		bot.comboBox(0).setSelection("platform");
		//bot.list().select(appName);

		
		bot.button("Finish").click();
		bot.waitUntil(Conditions.shellCloses(emptyTitleShell1[0]), 120000); // 最长等 2 分钟

		bot.viewByTitle("Project Explorer").show();
		bot.viewByTitle("Project Explorer").bot().tree().select("app").contextMenu("Reset Project").click();

		bot.comboBox().setSelection("ph1p35");
		bot.button("Reset").click();
		bot.button("Close").click();
	}

}
